Class Throttler

Summary

Fully Qualified Name: CodeIgniter\Throttle\Throttler
Implements: ThrottlerInterface

Description

Class Throttler

Uses an implementation of the Token Bucket algorithm to implement a "rolling window" type of throttling that can be used for rate limiting an API or any other request.

Each "token" in the "bucket" is equivalent to a single request for the purposes of this implementation.

Methods

Name Description Defined By
__construct() Constructor. Throttler
check() Restricts the number of requests made by a single IP address within a set number of seconds. Throttler
getTokenTime() Returns the number of seconds until the next available token will be released for usage. Throttler
setTestTime() Used during testing to set the current timestamp to use. Throttler
time() Return the test time, defaulting to current. Throttler

Method Details

__construct()

Constructor.

Parameter Name Type Description
$cache \type

Returns:

check()

Restricts the number of requests made by a single IP address within a set number of seconds.

Example:

if (! $throttler->check($request->ipAddress(), 60, MINUTE)) {

 die('You submitted over 60 requests within a minute.');

}

Parameter Name Type Description
$key string The
$capacity int The
$seconds int The
$cost int The

Returns: bool

getTokenTime()

Returns the number of seconds until the next available token will be released for usage.

Returns: int

setTestTime()

Used during testing to set the current timestamp to use.

Parameter Name Type Description
$time int

Returns: $this

time()

Return the test time, defaulting to current.

Returns: int

Top